package Utils

import (
	"encoding/binary"
	"net"
	"fmt"
)

func Int32ToBytes(i int32) []byte {
	var buf = make([]byte, 4)
	binary.BigEndian.PutUint32(buf, uint32(i))
	return buf
}

func BytesToInt32(buf []byte) int32 {
	return int32(binary.BigEndian.Uint32(buf))
}

func ConnectionExchange(src, dst net.Conn) {
	go transferData(src, dst)
	go transferData(dst, src)
}

// core
func transferData(src, dst net.Conn) {
	var buffer = make([]byte, 4096)
	for {
		size, err := src.Read(buffer)
		if err != nil {
			fmt.Println("error during transferData", err)
			src.Close()
			dst.Close()
			break
		}
		value := buffer[:size]

		_, err2 := dst.Write(value)
		if err2 != nil {
			fmt.Println("error during transferData", err2)
			src.Close()
			dst.Close()
			break
		}
	}
}
